<?php  

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Layout
{
    var $obj;
    var $layout;

    function Layout($layout = "layout_main")
    {
        $this->obj =& get_instance();
        $this->layout = $layout;
    }

    function setLayout($layout)
    {
      $this->layout = $layout;
    }

  

    /**
     * Function to view website page
     * @param $view
     * @param $data
     * @param $return
     * @return null
     */

    function view($view, $data=null, $return=false)
    {
        $loadedData = array();
        $data  = $this->getPaths($data);
        $data['base_path'] = $this->obj->config->item('base_path');
        $data['image_path'] = $this->obj->config->item('image_path');
        $loadedData['content_for_layout'] = $this->obj->load->view($view,$data,true);
        
       if($return)
        {
            $output = $this->obj->load->view($this->layout, $loadedData, true);
            return $output;
        }
        else
        {
            $this->obj->load->view($this->layout, $loadedData, false);
        }
    }
   
    /**
     * Function to show admininstration panel
     * @param $view
     * @param $data
     * @param $return
     * @return null
     */
	function adminview($view, $data=null, $return=false)
    {
        $this->setLayout('admin_layout_main');
    	$loadedData = array();
    	$data  = $this->getAdminPaths($data);
        $admin_view_folder = $this->obj->config->item('admin_view_path');
        $loadedData['menu'] = $this->getMenu($data);
        $loadedData['content_for_layout'] = $this->obj->load->view($admin_view_folder.$view,$data,true);
       
        if($return)
        {
            $output = $this->obj->load->view($admin_view_folder.$this->layout, $loadedData, true);
            return $output;
        }
        else
        {
            $this->obj->load->view($admin_view_folder.$this->layout, $loadedData, false);
        }
    }
	function memberview($view, $data=null, $return=false)
    {
        $this->setLayout('member_layout_main');
    	$loadedData = array();
    	$data  = $this->getMemberPaths($data);
        $member_view_folder = $this->obj->config->item('member_view_path');
        $loadedData['menu'] = $this->getMemberMenu($data);
        $loadedData['content_for_layout'] = $this->obj->load->view($member_view_folder.$view,$data,true);
       
        if($return)
        {
            $output = $this->obj->load->view($member_view_folder.$this->layout, $loadedData, true);
            return $output;
        }
        else
        {
            $this->obj->load->view($member_view_folder.$this->layout, $loadedData, false);
        }
    }
    
    /**
     * Fetch website paths
     * @param $data
     * @return null
     */
	function getPaths($data = array()) {
		$data['base_path'] 	= $this->obj->config->item('base_path');
		$data['base_url'] 	= $this->obj->config->item('base_url');
        $data['image_path'] = $this->obj->config->item('image_path');
        $data['js_path'] 	= $this->obj->config->item('js_path');
        $data['css_path'] 	= $this->obj->config->item('css_path');
        $data['upload_url'] = $this->obj->config->item('upload_url');
        $data['site_title'] = $this->obj->config->item('site_title');
        return $data;	

    }

    

    /**

     * Fetch Administration Paths

     * @param $data

     * @return null

     */

    function getAdminPaths($data = array()) {

    	$data = $this->getPaths($data);

		$data['admin_base_path'] = $this->obj->config->item('admin_base_path');

		$data['admin_base_url'] = $this->obj->config->item('admin_base_url');

        $data['admin_image_path'] = $this->obj->config->item('admin_image_path');

        $data['admin_js_path'] 	= $this->obj->config->item('admin_js_path');

        $data['admin_css_path'] 	= $this->obj->config->item('admin_css_path');

        return $data;	

    }

     function getMemberPaths($data = array()) {

    	$data = $this->getPaths($data);

		$data['member_base_path'] = $this->obj->config->item('member_base_path');

		$data['member_base_url'] = $this->obj->config->item('member_base_url');

        $data['member_image_path'] = $this->obj->config->item('member_image_path');

        $data['member_js_path'] 	= $this->obj->config->item('member_js_path');

        $data['member_css_path'] 	= $this->obj->config->item('member_css_path');

        return $data;	

    }

    function getMenu($data = array()) {

    	$admin_view_folder = $this->obj->config->item('admin_view_path');

    	return $this->obj->load->view($admin_view_folder.'menu.php',$data,true);

    }
	function getMemberMenu($data = array()) {

    	$member_view_folder = $this->obj->config->item('member_view_path');

    	return $this->obj->load->view($member_view_folder.'menu.php',$data,true);

    }


    

/**

     * Function to view website page

     * @param $view

     * @param $data

     * @param $return

     * @return null

     */

    function getView($view, $data=null, $return=false)

    {

        $loadedData = array();
		
        $loadedData['menu'] = $this->getMenu($data);
		
        $data  = $this->getPaths($data);

        $data  = $this->getAdminPaths($data);

        $data['base_path'] = $this->obj->config->item('base_path');

        $data['image_path'] = $this->obj->config->item('image_path');

        $output = $this->obj->load->view($view,$data,true);

        

        if($return)

        {

            return $output;

        }

        else

        {

            echo $output;

        }

    }

    

}